
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: '/login',
    component: Home,
    children: [
      {
        path: '/system/user',
        name: 'User',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/system/user.vue')
      },
      {
        path: '/system/menu',
        name: "Menu",
        component: () => import('../views/system/menu.vue')
      },
      {
        path: '/system/role',
        name: "Role",
        component: () => import('../views/system/role.vue')
      },
      {
        path: '/system/dept',
        name: "Dept",
        component: () => import('../views/system/dept.vue')
      }
      ,
    ]
  },

  {
    path: '/login',
    name: "Login",
    component: () => import('../views/login.vue')
  }
]

const router = new VueRouter({
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
  } else {
    let token = localStorage.getItem('token');
    if (token === null || token === '') {
      next('/login')
    } else {
      next();
    }
  }
})

export default router
